//
//  PGTabBarController.m
//  youqu
//
//  Created by 彭鹏 on 16/9/3.
//  Copyright © 2016年 pengpeng. All rights reserved.
//



#import "PGTabBarController.h"
#import "PGEssenceViewController.h"
#import "PGFriendTrendsViewController.h"
#import "PGPublishViewController.h"
#import "PGNewViewController.h"
#import "PGMeViewController.h"

#import "PGNavigationController.h"




@interface PGTabBarController ()

@end

@implementation PGTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
    //添加自控制器
    [self addChildController];

    [self setupPlusButton];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        
//    });
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [self setupPlusButton];
}

+ (void)initialize {
    if ([PGTabBarController class] != self) return;
        
        
    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedIn:[PGTabBarController class], nil];
    //    [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    [tabBarItem setTitleTextAttributes:@{
                                         NSFontAttributeName : [UIFont systemFontOfSize:13.0],
                                         NSForegroundColorAttributeName : [UIColor grayColor]
                                         } forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:@{
                                         NSForegroundColorAttributeName : [UIColor blackColor]
                                         } forState:UIControlStateSelected];
    
    
    
}

//添加自控制器
- (void)addChildController {
    
    //精华
    PGEssenceViewController *essence = [[PGEssenceViewController alloc] init];
    //    essence.view.backgroundColor = [UIColor redColor];
    [self setupChildController:essence image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon" title:@"精华"];
    

    
    //新帖
    PGNewViewController *new = [[PGNewViewController alloc] init];
    //    new.view.backgroundColor = [UIColor greenColor];
    [self setupChildController:new image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon" title:@"新帖"];
    
    
    //加号
    PGPublishViewController *plus = [[PGPublishViewController alloc] init];
//    plus.view.backgroundColor = [UIColor brownColor];
    plus.tabBarItem.enabled = NO;
//    plus.tabBarItem.image = [UIImage imageNamed:@"tabBar_publish_icon"];
//    plus.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_publish_click_icon"];
//    plus.tabBarItem.imageInsets = UIEdgeInsetsMake(7, 0, -7, 0);
    [self addChildViewController:plus];
    
    
    
    //关注
    PGFriendTrendsViewController *friendTrends = [[PGFriendTrendsViewController alloc] init];
    //    friendTrends.view.backgroundColor = [UIColor blueColor];
    [self setupChildController:friendTrends image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon" title:@"关注"];
    

    

    //我
    UIStoryboard *meStoryboard = [UIStoryboard storyboardWithName:NSStringFromClass([PGMeViewController class]) bundle:nil];
    PGMeViewController *me = [meStoryboard instantiateInitialViewController];
    [self setupChildController:me image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon" title:@"我"];
    
}

-(void)setupChildController:(UIViewController *)viewController image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title {
    PGNavigationController *navigationController = [[PGNavigationController alloc] initWithRootViewController:viewController];
    navigationController.tabBarItem.image = [UIImage imageNamed:image];
    navigationController.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    viewController.title = title;
    
    [self addChildViewController:navigationController];
}

//创建加号按钮
- (void)setupPlusButton {
    UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [plusButton setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
    [plusButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
    [plusButton sizeToFit];
    plusButton.center = CGPointMake(SCREENW*0.5, self.tabBar.pg_height*0.5);
    //    [UIScreen mainScreen].bounds.size.height;
    [self.tabBar addSubview:plusButton];
}

@end
